package com.qianli.user.domain.respository;

import com.fqgj.xjd.user.dao.UserContactAddressBookDao;
import com.fqgj.xjd.user.dao.UserContactEmergencyDao;
import com.fqgj.xjd.user.entity.UserContactAddressBookEntity;
import com.fqgj.xjd.user.entity.UserContactEmergencyEntity;
import com.qianli.user.domain.enums.UserModelItemStatusEnum;
import com.qianli.user.domain.model.UserSocialModel;
import com.qianli.user.domain.model.social.UserContactAddressBook;
import com.qianli.user.domain.model.social.UserContactEmergency;
import com.qianli.user.ro.social.UserContactAddressBookRO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * name:社交信息
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 18:04
 * version: v1.0.0
 **/
@Repository
public class UserSocialRespository implements BaseRespository<UserSocialModel>{

    @Autowired
    private UserContactAddressBookDao userContactAddressBookDao;

    @Autowired
    private UserContactEmergencyDao userContactEmergencyDao;

    /**
     * 加载社交信息（通讯录+紧急联系人）
     * @param model
     */
    @Override
    public void load(UserSocialModel model) {
        String userCode = model.getUserCode();
        //通讯录
        UserContactAddressBookEntity addressBookEntity = userContactAddressBookDao.selectByUserCode(userCode);
        if(null != addressBookEntity){
            UserContactAddressBook book = new UserContactAddressBook();
            BeanUtils.copyProperties(addressBookEntity,book);
            model.setUserContactAddressBook(book);
        }
        //紧急联系人
        UserContactEmergencyEntity emergencyEntity = userContactEmergencyDao.selectByUserCode(userCode);
        if(null != emergencyEntity){
            UserContactEmergency emergency = new UserContactEmergency();
            BeanUtils.copyProperties(emergencyEntity,emergency);
            model.setUserContactEmergency(emergency);
        }
    }

    /**
     * 保存模型
     * @param model
     */
    @Override
    public void store(UserSocialModel model) {
        //通讯录
        if(null != model.getUserContactAddressBook()){
            UserContactAddressBookEntity bookEntity = new UserContactAddressBookEntity();
            BeanUtils.copyProperties(model.getUserContactAddressBook(),bookEntity);
            if(model.getUserContactAddressBook().getItemStatus().equals(UserModelItemStatusEnum.ADD.getType())){
                userContactAddressBookDao.insert(bookEntity);
            }else {
                userContactAddressBookDao.updateByPrimaryKey(bookEntity);
            }

        }
        //紧急联系人
        if(null != model.getUserContactEmergency()){
            UserContactEmergencyEntity emergencyEntity = new UserContactEmergencyEntity();
            BeanUtils.copyProperties(model.getUserContactEmergency(),emergencyEntity);
            if(model.getUserContactEmergency().getItemStatus().equals(UserModelItemStatusEnum.ADD.getType())){
                userContactEmergencyDao.insert(emergencyEntity);
            }else{
                userContactEmergencyDao.updateByPrimaryKey(emergencyEntity);
            }
        }

    }

    @Override
    public void restore(UserSocialModel model) {

    }
}
